package com.zczc.service.impl;

import com.github.pagehelper.PageInfo;
import com.zczc.dao.api.MessageGetterDao;
import com.zczc.dao.api.MessageInfoDao;
import com.zczc.dao.api.MessageUserDao;
import com.zczc.entity.MessageGetter;
import com.zczc.entity.MessageInfo;
import com.zczc.entity.MessageUser;
import com.zczc.service.MessageGetterService;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 9:30 2018-12-13
 * @Modify by:
 */
@Service
public class MessageGetterServiceImpl implements MessageGetterService {

  private final static Logger log= LoggerFactory.getLogger(MessageGetterServiceImpl.class);

  @Autowired
  private MessageGetterDao messageGetterDao;

  @Autowired
  private MessageUserDao messageUserDao;

  @Autowired
  private MessageInfoDao messageInfoDao;

  @Override
  public Boolean addMessageGetterSelective(MessageGetter messageGetter) throws Exception {
    Exception exception=null;
    boolean flag=false;
    if(messageGetter!=null){
      if(messageGetter!=null){
        //根据用户id查询消息推送表中的消息id
        List<MessageUser> MessageUsers = messageUserDao
            .getAllMessageInfoId(messageGetter.getUserId());
        if(MessageUsers!=null && MessageUsers.size()>0){
          for (MessageUser messageUser : MessageUsers) {
            //根据根据消息id值结合推送时间查询到所有的推送消息
            MessageInfo messageInfo=new MessageInfo();
            messageInfo.setId(messageUser.getMessageId());
            messageInfo.setPushTime(Calendar.getInstance().getTime());
            List<MessageInfo> messageInfos=messageInfoDao.selectMessageInfoByIdAndTime(messageInfo);
              if(messageInfos!=null && messageInfos.size()>0){
                //进行批量添加
                for (MessageInfo info : messageInfos) {
                  messageGetter.setId(UUID.randomUUID().toString());
                  messageGetter.setDeleted(false);
                  messageGetter.setReaded(false);
                  messageGetter.setMessageTitle(info.getTitle());
                  messageGetter.setMessageContent(info.getContent());
                  messageGetter.setMessageType(info.getMessageType());
                  messageGetter.setMessageId(info.getId());
                  messageGetter.setMessageUrl(info.getMessageUrl());
                  messageGetter.setPushTime(Calendar.getInstance().getTime());
                  try {
                    flag = messageGetterDao.addMessageGetter(messageGetter)>0;

                    //如果向用户表中添加消息成功，则应该相应的改变message_user中的字段，added;避免重复添加
                    if(flag){
                      messageUserDao.addMessageSuccess(info.getId());

                    }

                  } catch (Exception e) {
                    log.info("获取站内消息失败的原因为："+e.getMessage());
                  }
                  if(!flag){
                    exception=new Exception("获取站内消息失败！");
                    throw exception;
                  }
                }
              }
          }
        }
      }
    }
    return flag;
  }

  @Override
  public PageInfo<MessageGetter> selectMessageByUserId(MessageGetter messageGetter, Integer pageNum,
      Integer pageSize) throws Exception {
    if(messageGetter!=null){
      if(messageGetter.getUserId()!=null){
        //先成功添加消息，在获取消息
        addMessageGetterSelective(messageGetter);
        pageNum=pageNum==null?1:pageNum;
        pageSize=pageSize==null?20:pageSize;
       return messageGetterDao.selectMessageByPage(messageGetter,pageNum,pageSize);
      }
    }
    return null;
  }

  @Override
  public Boolean deleteMessageByIdLogic(List<String> ids, String userId) {
    if(ids!=null && ids.size()>0 && userId !=null){
      MessageGetter messageGetter=new MessageGetter();
      messageGetter.setDeleted(true);
      messageGetter.setUserId(userId);
      return messageGetterDao.deleteMessageByIdLogic(ids,messageGetter)>0;
    }
    return false;
  }

  @Override
  public Boolean readnMessageById(List<String> ids, String userId) {
    if(ids!=null && ids.size()>0 && userId!=null){
      MessageGetter messageGetter=new MessageGetter();
      messageGetter.setUserId(userId);
      messageGetter.setReaded(true);
      return messageGetterDao.readMessageById(ids,messageGetter)>0;
    }
    return false;
  }
}
